package com.pda.xinmazhi.ui.machinecall;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import com.pda.xinmazhi.R;
import com.pda.xinmazhi.adapter.MachineCallAdapter;
import com.pda.xinmazhi.bean.LatheMachinBean;
import com.pda.xinmazhi.bean.LatheOrderBean;
import com.pda.xinmazhi.bean.ScanBean;
import com.pda.xinmazhi.bean.SelectBean;
import com.pda.xinmazhi.contract.MachineCallContract;
import com.pda.xinmazhi.dialog.SelectOrderDialog;
import com.pda.xinmazhi.presenter.MachineCallPresenter;
import com.pda.xinmazhi.utils.PopUtisl;
import com.pda.xinmazhi.view.ScanButView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NoCallFragment extends BaseMvpFragment<MachineCallPresenter> implements MachineCallContract.View {
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    SelectBean faceBean, latheBean;
    MachineCallAdapter machineCallAdapter;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;

    @Override
    public MachineCallPresenter getPresenter() {
        return new MachineCallPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_nocall;
    }

    @Override
    public void initView() {
        srlLayout.setEnableLoadMore(false);
        srlLayout.setEnableRefresh(false);
        machineCallAdapter = new MachineCallAdapter(R.layout.item_machine_call, new ArrayList<>());
        rvContent.setAdapter(machineCallAdapter);
        // presenter.getOrderListByLathe("");
        //presenter.getCenterByUserId();
    }

    @Override
    public void initLinsenterner() {
        machineCallAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                LatheOrderBean orderBean = machineCallAdapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable(StringKey.STORAGEBIN, orderBean);
                startIntent(AppActivityKey.MACHINECALLTABACTIVITY, bundle);
            }
        });
        /*sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (!TextUtils.isEmpty(scanBean.getCode())) {
                    if (scanBean.getCode().contains("order")) {
                        int lenth = scanBean.getCode().indexOf("A");
                        String order = scanBean.getCode().substring(lenth + 1);
                        if (latheBean != null)
                            presenter.getOrderListByLathe(latheBean.getId(), order);
                    }
                }


            }
        });*/
    }

    @Override
    public void getData() {

    }

   /* @Override
    public void setFaceList(List<WorkSurfaceBean> list) {
        if (list.size() > 0 && TextUtils.isEmpty(tvCenter.getText())){
            SelectBean selectBean=new SelectBean(list.get(0).getCenterName(), list.get(0).getCenterCode());
            faceBean = selectBean;
            tvCenter.setText(selectBean.getName());
            latheBean = null;
            tvMachine.setText("");
            return;
        }
        List<SelectBean> selectBeans = new ArrayList<>();
        for (WorkSurfaceBean bean : list) {
            selectBeans.add(new SelectBean(bean.getCenterName(), bean.getCenterCode()));
        }
        PopUtisl.showSelectPop(mContext, selectBeans, tvCenter, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                faceBean = bean;
                tvCenter.setText(bean.getName());
                latheBean = null;
                tvMachine.setText("");
            }
        }, "请选择工作中心");
    }*/

    @Override
    public void setScanData(List<LatheOrderBean> list) {
        if (list != null && list.size() > 0) {
            if (list.size() == 1) {
                LatheOrderBean orderBean = list.get(0);
                Bundle bundle = new Bundle();
                bundle.putSerializable(StringKey.STORAGEBIN, orderBean);
                startIntent(AppActivityKey.MACHINECALLTABACTIVITY, bundle);
            } else {
                SelectOrderDialog selectOrderDialog = new SelectOrderDialog(mContext, new SelectOrderDialog.OnSelectClick() {
                    @Override
                    public void onSelect(LatheOrderBean latheOrderBean) {
                        if (latheOrderBean != null) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(StringKey.STORAGEBIN, latheOrderBean);
                            startIntent(AppActivityKey.MACHINECALLTABACTIVITY, bundle);
                        }
                    }
                });
                selectOrderDialog.show(list);
            }
        }

    }

    @Override
    public void setLatheList(List<LatheMachinBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LatheMachinBean bean : list) {
            selectBeans.add(new SelectBean(bean.getLatheName(), bean.getLatheCode()));
        }
        PopUtisl.showSelectPop(mContext, selectBeans, tvMachine, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                latheBean = bean;
                tvMachine.setText(bean.getName());
                presenter.getOrderListByLathe(latheBean.getId());
            }
        }, "请选择机台");
    }

    @Override
    public void onResume() {
        super.onResume();
        latheBean=null;
        tvMachine.setText("");
        machineCallAdapter.setNewInstance(new ArrayList<>());
        /*if (latheBean != null) {
            presenter.getOrderListByLathe(latheBean.getId());
        }*/
    }

    @Override
    public void setDataList(List<LatheOrderBean> list) {
        machineCallAdapter.setNewInstance(list);
    }

    @OnClick({R.id.tv_machine, R.id.tv_center})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_machine:
                /*if (TextUtils.isEmpty(tvCenter.getText())) {
                    showMessage("请先选择工作中心");
                    return;
                }*/
                presenter.getLatheListByCenter("JM1001");
                break;
            case R.id.tv_center:
                presenter.getCenterByUserId();
                break;
        }
    }

    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
// 关闭软键盘
        if (imm != null) {
            imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
        }
        if (latheBean == null) {
            showMessage("请先选择机台");
            return;
        }
        if (TextUtils.isEmpty(etSearch.getText())){
            ToastUtils.showToast(mContext, "搜素内容不能为空！");
            return;
        }
        presenter.getOrderSearchListByLathe(latheBean.getId(),etSearch.getText().toString());
    }
}
